package com.example.gulimall.product.service.impl;


import com.example.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.vo.AttrGroupRelationVo;
import com.example.gulimall.product.vo.CategoryWithAttrResVo;
import com.example.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.product.dao.AttrGroupDao;
import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        if (catId==0){
            QueryWrapper<AttrGroupEntity> wrapper=new QueryWrapper<AttrGroupEntity>();
            String key=params.get("key").toString();
            if (!StringUtils.isEmpty(key)){
                wrapper.eq("attr_group_id",key).or().like("attr_group_name",key).or()
                        .like("descript",key);
            }

            IPage<AttrGroupEntity> page = this.page(
                    //分页
                    new Query<AttrGroupEntity>().getPage(params),
                    //条件构造器
                    wrapper
            );
            return new PageUtils(page);
        }else {
            String key=(String) params.get("key");
            //mybaties-plus 条件构造器
            QueryWrapper<AttrGroupEntity> wrapper=new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catId);
            if (!StringUtils.isEmpty(key)){
                //函数式编程构造条件 catelog_id=catId and (attr_group_id=key or attr_group_name like key)
                wrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public void deleteRelation(List<AttrGroupRelationVo> vos) {
        //自定义与数据库交互一次达到批量删除的效果
        relationDao.deleteBatchRelaton(vos);

    }

    /**
     * 根据分类id查询该分类下所有分组，在通过分组id该分组下所有属性
     * 封装成一个集合
     * @param catelogId
     * @return
     */
    @Override
    public List<CategoryWithAttrResVo> getWithattr( Long catelogId) {

        //1、查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<CategoryWithAttrResVo> collect = attrGroupEntities.stream().map(group -> {
            CategoryWithAttrResVo attrsVo = new CategoryWithAttrResVo();
            BeanUtils.copyProperties(group,attrsVo);
            //查询对应分组下所有属性
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithBySpuId(Long spuId, Long catalogId) {

        //1 查出当前spu对应的所有属性的分组信息 以及当前分组的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> voList= baseMapper.getAttrGroupWithBySpuId(spuId,catalogId);
        return  voList;
    }


}